{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Image Pre-processing\n",
    "\n",
    "### Overview\n",
    "\n",
    "This is the second step for data preparation\n",
    "\n",
    "Input: `.nii`-like images and labels converted from `dicom`s/ `png` files\n",
    "\n",
    "Output: image-labels with unified size (axial), voxel-spacing, and alleviated off-resonance effects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n"
     ]
    }
   ],
   "source": [
    "%reset\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import os\n",
    "import glob\n",
    "import SimpleITK as sitk\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import copy\n",
    "import sys\n",
    "sys.path.insert(0, '../../dataloaders/')\n",
    "import niftiio as nio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "IMG_FOLDER = \"./bSSFP\" #, path of nii-like images from step 1\n",
    "OUT_FOLDER=\"./cmr_MR_normalized/\" # output directory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**0. Find images and their ground-truth segmentations**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgs = glob.glob(IMG_FOLDER + f'/image_*.nii.gz')\n",
    "imgs = [ fid for fid in sorted(imgs) ]\n",
    "segs = [ fid for fid in sorted(glob.glob(IMG_FOLDER + f'/label_*.nii.gz')) ]\n",
    "\n",
    "pids = [pid.split(\"_\")[-1].split(\".\")[0] for pid in imgs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./bSSFP/image_1.nii.gz',\n",
       " './bSSFP/image_10.nii.gz',\n",
       " './bSSFP/image_11.nii.gz',\n",
       " './bSSFP/image_12.nii.gz',\n",
       " './bSSFP/image_13.nii.gz',\n",
       " './bSSFP/image_14.nii.gz',\n",
       " './bSSFP/image_15.nii.gz',\n",
       " './bSSFP/image_16.nii.gz',\n",
       " './bSSFP/image_17.nii.gz',\n",
       " './bSSFP/image_18.nii.gz',\n",
       " './bSSFP/image_19.nii.gz',\n",
       " './bSSFP/image_2.nii.gz',\n",
       " './bSSFP/image_20.nii.gz',\n",
       " './bSSFP/image_21.nii.gz',\n",
       " './bSSFP/image_22.nii.gz',\n",
       " './bSSFP/image_23.nii.gz',\n",
       " './bSSFP/image_24.nii.gz',\n",
       " './bSSFP/image_25.nii.gz',\n",
       " './bSSFP/image_26.nii.gz',\n",
       " './bSSFP/image_27.nii.gz',\n",
       " './bSSFP/image_28.nii.gz',\n",
       " './bSSFP/image_29.nii.gz',\n",
       " './bSSFP/image_3.nii.gz',\n",
       " './bSSFP/image_30.nii.gz',\n",
       " './bSSFP/image_31.nii.gz',\n",
       " './bSSFP/image_32.nii.gz',\n",
       " './bSSFP/image_33.nii.gz',\n",
       " './bSSFP/image_34.nii.gz',\n",
       " './bSSFP/image_35.nii.gz',\n",
       " './bSSFP/image_4.nii.gz',\n",
       " './bSSFP/image_5.nii.gz',\n",
       " './bSSFP/image_6.nii.gz',\n",
       " './bSSFP/image_7.nii.gz',\n",
       " './bSSFP/image_8.nii.gz',\n",
       " './bSSFP/image_9.nii.gz']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./bSSFP/label_1.nii.gz',\n",
       " './bSSFP/label_10.nii.gz',\n",
       " './bSSFP/label_11.nii.gz',\n",
       " './bSSFP/label_12.nii.gz',\n",
       " './bSSFP/label_13.nii.gz',\n",
       " './bSSFP/label_14.nii.gz',\n",
       " './bSSFP/label_15.nii.gz',\n",
       " './bSSFP/label_16.nii.gz',\n",
       " './bSSFP/label_17.nii.gz',\n",
       " './bSSFP/label_18.nii.gz',\n",
       " './bSSFP/label_19.nii.gz',\n",
       " './bSSFP/label_2.nii.gz',\n",
       " './bSSFP/label_20.nii.gz',\n",
       " './bSSFP/label_21.nii.gz',\n",
       " './bSSFP/label_22.nii.gz',\n",
       " './bSSFP/label_23.nii.gz',\n",
       " './bSSFP/label_24.nii.gz',\n",
       " './bSSFP/label_25.nii.gz',\n",
       " './bSSFP/label_26.nii.gz',\n",
       " './bSSFP/label_27.nii.gz',\n",
       " './bSSFP/label_28.nii.gz',\n",
       " './bSSFP/label_29.nii.gz',\n",
       " './bSSFP/label_3.nii.gz',\n",
       " './bSSFP/label_30.nii.gz',\n",
       " './bSSFP/label_31.nii.gz',\n",
       " './bSSFP/label_32.nii.gz',\n",
       " './bSSFP/label_33.nii.gz',\n",
       " './bSSFP/label_34.nii.gz',\n",
       " './bSSFP/label_35.nii.gz',\n",
       " './bSSFP/label_4.nii.gz',\n",
       " './bSSFP/label_5.nii.gz',\n",
       " './bSSFP/label_6.nii.gz',\n",
       " './bSSFP/label_7.nii.gz',\n",
       " './bSSFP/label_8.nii.gz',\n",
       " './bSSFP/label_9.nii.gz']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "segs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1. Unify image sizes and roi**\n",
    "\n",
    "a. Cut bright end of histogram to alleviate off-resonance issue\n",
    "\n",
    "b. Resample images to unified spacing\n",
    "\n",
    "c. Crop ROIs out to unify image sizes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# some helper functions\n",
    "def resample_by_res(mov_img_obj, new_spacing, interpolator = sitk.sitkLinear, logging = True):\n",
    "    resample = sitk.ResampleImageFilter()\n",
    "    resample.SetInterpolator(interpolator)\n",
    "    resample.SetOutputDirection(mov_img_obj.GetDirection())\n",
    "    resample.SetOutputOrigin(mov_img_obj.GetOrigin())\n",
    "    mov_spacing = mov_img_obj.GetSpacing()\n",
    "    \n",
    "    if len(new_spacing) != 3:\n",
    "        new_spacing = [new_spacing[0], new_spacing[1], mov_spacing[2]]\n",
    "\n",
    "    resample.SetOutputSpacing(new_spacing)\n",
    "    RES_COE = np.array(mov_spacing) * 1.0 / np.array(new_spacing)\n",
    "    new_size = np.array(mov_img_obj.GetSize()) *  RES_COE \n",
    "\n",
    "    resample.SetSize( [int(sz+1) for sz in new_size] )\n",
    "    if logging:\n",
    "        print(\"Spacing: {} -> {}\".format(mov_spacing, new_spacing))\n",
    "        print(\"Size {} -> {}\".format( mov_img_obj.GetSize(), new_size ))\n",
    "\n",
    "    return resample.Execute(mov_img_obj)\n",
    "\n",
    "def resample_lb_by_res(mov_lb_obj, new_spacing, interpolator = sitk.sitkLinear, ref_img = None, logging = True):\n",
    "    \n",
    "    mov_spacing = mov_lb_obj.GetSpacing()\n",
    "    if len(new_spacing) != 3:\n",
    "        new_spacing = [new_spacing[0], new_spacing[1], mov_spacing[2]]\n",
    "        \n",
    "    src_mat = sitk.GetArrayFromImage(mov_lb_obj)\n",
    "    lbvs = np.unique(src_mat)\n",
    "    if logging:\n",
    "        print(\"Label values: {}\".format(lbvs))\n",
    "    for idx, lbv in enumerate(lbvs):\n",
    "        _src_curr_mat = np.float32(src_mat == lbv) \n",
    "        _src_curr_obj = sitk.GetImageFromArray(_src_curr_mat)\n",
    "        _src_curr_obj.CopyInformation(mov_lb_obj)\n",
    "        _tar_curr_obj = resample_by_res( _src_curr_obj, new_spacing, interpolator, logging )\n",
    "        _tar_curr_mat = np.rint(sitk.GetArrayFromImage(_tar_curr_obj)) * lbv\n",
    "        if idx == 0:\n",
    "            out_vol = _tar_curr_mat\n",
    "        else:\n",
    "            out_vol[_tar_curr_mat == lbv] = lbv\n",
    "    out_obj = sitk.GetImageFromArray(out_vol)\n",
    "    out_obj.SetSpacing( _tar_curr_obj.GetSpacing() )\n",
    "    if ref_img != None:\n",
    "        out_obj.CopyInformation(ref_img)\n",
    "    return out_obj\n",
    "        \n",
    "def get_label_center(label):\n",
    "    nnz = np.sum(label > 1e-5)\n",
    "    return np.int32(np.rint(np.sum(np.nonzero(label), axis = 1) * 1.0 / nnz))\n",
    "\n",
    "def image_crop(ori_vol, crop_size, referece_ctr_idx, padval = 0., only_2d = True):\n",
    "    \"\"\" crop a 3d matrix given the index of the new volume on the original volume\n",
    "        Args:\n",
    "            refernce_ctr_idx: the center of the new volume on the original volume (in indices)\n",
    "            only_2d: only do cropping on first two dimensions\n",
    "    \"\"\"\n",
    "    _expand_cropsize = [x + 1 for x in crop_size] # to deal with boundary case\n",
    "    if only_2d:\n",
    "        assert len(crop_size) == 2, \"Actual len {}\".format(len(crop_size))\n",
    "        assert len(referece_ctr_idx) == 2, \"Actual len {}\".format(len(referece_ctr_idx))\n",
    "        _expand_cropsize.append(ori_vol.shape[-1])\n",
    "        \n",
    "    image_patch = np.ones(tuple(_expand_cropsize)) * padval\n",
    "\n",
    "    half_size = tuple( [int(x * 1.0 / 2) for x in _expand_cropsize] )\n",
    "    _min_idx = [0,0,0]\n",
    "    _max_idx = list(ori_vol.shape)\n",
    "\n",
    "    # bias of actual cropped size to the beginning and the end of this volume\n",
    "    _bias_start = [0,0,0]\n",
    "    _bias_end = [0,0,0]\n",
    "\n",
    "    for dim,hsize in enumerate(half_size):\n",
    "        if dim == 2 and only_2d:\n",
    "            break\n",
    "\n",
    "        _bias_start[dim] = np.min([hsize, referece_ctr_idx[dim]])\n",
    "        _bias_end[dim] = np.min([hsize, ori_vol.shape[dim] - referece_ctr_idx[dim]])\n",
    "\n",
    "        _min_idx[dim] = referece_ctr_idx[dim] - _bias_start[dim]\n",
    "        _max_idx[dim] = referece_ctr_idx[dim] + _bias_end[dim]\n",
    "        \n",
    "    if only_2d:\n",
    "        image_patch[ half_size[0] - _bias_start[0]: half_size[0] +_bias_end[0], \\\n",
    "                half_size[1] - _bias_start[1]: half_size[1] +_bias_end[1], ... ] = \\\n",
    "                ori_vol[ referece_ctr_idx[0] - _bias_start[0]: referece_ctr_idx[0] +_bias_end[0], \\\n",
    "                referece_ctr_idx[1] - _bias_start[1]: referece_ctr_idx[1] +_bias_end[1], ... ]\n",
    "\n",
    "        image_patch = image_patch[ 0: crop_size[0], 0: crop_size[1], : ]\n",
    "    # then goes back to original volume\n",
    "    else:\n",
    "        image_patch[ half_size[0] - _bias_start[0]: half_size[0] +_bias_end[0], \\\n",
    "                half_size[1] - _bias_start[1]: half_size[1] +_bias_end[1], \\\n",
    "                half_size[2] - _bias_start[2]: half_size[2] +_bias_end[2] ] = \\\n",
    "                ori_vol[ referece_ctr_idx[0] - _bias_start[0]: referece_ctr_idx[0] +_bias_end[0], \\\n",
    "                referece_ctr_idx[1] - _bias_start[1]: referece_ctr_idx[1] +_bias_end[1], \\\n",
    "                referece_ctr_idx[2] - _bias_start[2]: referece_ctr_idx[2] +_bias_end[2] ]\n",
    "\n",
    "        image_patch = image_patch[ 0: crop_size[0], 0: crop_size[1], 0: crop_size[2] ]\n",
    "    return image_patch\n",
    "\n",
    "def copy_spacing_ori(src, dst):\n",
    "    dst.SetSpacing(src.GetSpacing())\n",
    "    dst.SetOrigin(src.GetOrigin())\n",
    "    dst.SetDirection(src.GetDirection())\n",
    "    return dst\n",
    "\n",
    "s2n = sitk.GetArrayFromImage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Spacing: (1.25, 1.25, 11.999996185302734) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          18.02252243]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 11.999996185302734) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          18.02252243]\n",
      "Spacing: (1.25, 1.25, 11.999996185302734) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          18.02252243]\n",
      "Spacing: (1.25, 1.25, 11.999996185302734) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          18.02252243]\n",
      "Spacing: (1.25, 1.25, 11.999996185302734) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          18.02252243]\n",
      "./cmr_MR_normalized/image_1.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 13.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         16.2703415]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 13.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         16.2703415]\n",
      "Spacing: (1.25, 1.25, 13.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         16.2703415]\n",
      "Spacing: (1.25, 1.25, 13.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         16.2703415]\n",
      "Spacing: (1.25, 1.25, 13.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         16.2703415]\n",
      "./cmr_MR_normalized/image_10.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "./cmr_MR_normalized/image_11.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "./cmr_MR_normalized/image_12.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "./cmr_MR_normalized/image_13.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501806]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501806]\n",
      "Spacing: (1.25, 1.25, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501806]\n",
      "Spacing: (1.25, 1.25, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501806]\n",
      "Spacing: (1.25, 1.25, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501806]\n",
      "./cmr_MR_normalized/image_14.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          10.01251147]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          10.01251147]\n",
      "Spacing: (1.25, 1.25, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          10.01251147]\n",
      "Spacing: (1.25, 1.25, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          10.01251147]\n",
      "Spacing: (1.25, 1.25, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          10.01251147]\n",
      "./cmr_MR_normalized/image_15.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 7.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501734]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 7.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501734]\n",
      "Spacing: (1.25, 1.25, 7.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501734]\n",
      "Spacing: (1.25, 1.25, 7.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501734]\n",
      "Spacing: (1.25, 1.25, 7.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01501734]\n",
      "./cmr_MR_normalized/image_16.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "./cmr_MR_normalized/image_17.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501663]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501663]\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501663]\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501663]\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501663]\n",
      "./cmr_MR_normalized/image_18.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [288.         288.          12.01502021]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [288.         288.          12.01502021]\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [288.         288.          12.01502021]\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [288.         288.          12.01502021]\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [288.         288.          12.01502021]\n",
      "./cmr_MR_normalized/image_19.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502307]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502307]\n",
      "Spacing: (1.1875, 1.1875, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502307]\n",
      "Spacing: (1.1875, 1.1875, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502307]\n",
      "Spacing: (1.1875, 1.1875, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502307]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./cmr_MR_normalized/image_2.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01752524]\n",
      "./cmr_MR_normalized/image_20.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.        304.         14.0175194]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.        304.         14.0175194]\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.        304.         14.0175194]\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.        304.         14.0175194]\n",
      "Spacing: (1.1875, 1.1875, 7.999998569488525) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.        304.         14.0175194]\n",
      "./cmr_MR_normalized/image_21.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "./cmr_MR_normalized/image_22.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 11.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721259]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 11.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721259]\n",
      "Spacing: (1.25, 1.25, 11.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721259]\n",
      "Spacing: (1.25, 1.25, 11.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721259]\n",
      "Spacing: (1.25, 1.25, 11.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721259]\n",
      "./cmr_MR_normalized/image_23.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502021]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502021]\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502021]\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502021]\n",
      "Spacing: (1.25, 1.25, 8.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502021]\n",
      "./cmr_MR_normalized/image_24.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 12.000005722045898) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.        256.         12.0150245]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 12.000005722045898) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.        256.         12.0150245]\n",
      "Spacing: (1.25, 1.25, 12.000005722045898) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.        256.         12.0150245]\n",
      "Spacing: (1.25, 1.25, 12.000005722045898) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.        256.         12.0150245]\n",
      "Spacing: (1.25, 1.25, 12.000005722045898) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.        256.         12.0150245]\n",
      "./cmr_MR_normalized/image_25.nii.gz has been saved\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01501806]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01501806]\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01501806]\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01501806]\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01501806]\n",
      "./cmr_MR_normalized/image_26.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "Spacing: (1.25, 1.25, 11.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720901]\n",
      "./cmr_MR_normalized/image_27.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 8.0) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501877]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 8.0) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501877]\n",
      "Spacing: (1.1875, 1.1875, 8.0) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501877]\n",
      "Spacing: (1.1875, 1.1875, 8.0) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501877]\n",
      "Spacing: (1.1875, 1.1875, 8.0) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01501877]\n",
      "./cmr_MR_normalized/image_28.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 11.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 9) -> [256.         256.          13.51689505]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 11.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 9) -> [256.         256.          13.51689505]\n",
      "Spacing: (1.25, 1.25, 11.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 9) -> [256.         256.          13.51689505]\n",
      "Spacing: (1.25, 1.25, 11.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 9) -> [256.         256.          13.51689505]\n",
      "Spacing: (1.25, 1.25, 11.999999046325684) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 9) -> [256.         256.          13.51689505]\n",
      "./cmr_MR_normalized/image_29.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 8.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.        256.         12.0150245]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 8.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.        256.         12.0150245]\n",
      "Spacing: (1.25, 1.25, 8.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.        256.         12.0150245]\n",
      "Spacing: (1.25, 1.25, 8.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.        256.         12.0150245]\n",
      "Spacing: (1.25, 1.25, 8.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.        256.         12.0150245]\n",
      "./cmr_MR_normalized/image_3.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01751605]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01751605]\n",
      "Spacing: (1.1875, 1.1875, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01751605]\n",
      "Spacing: (1.1875, 1.1875, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01751605]\n",
      "Spacing: (1.1875, 1.1875, 7.999996662139893) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 14) -> [304.         304.          14.01751605]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./cmr_MR_normalized/image_30.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 11.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721379]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 11.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721379]\n",
      "Spacing: (1.25, 1.25, 11.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721379]\n",
      "Spacing: (1.25, 1.25, 11.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721379]\n",
      "Spacing: (1.25, 1.25, 11.000003814697266) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76721379]\n",
      "./cmr_MR_normalized/image_31.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 12.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          15.01877466]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 12.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          15.01877466]\n",
      "Spacing: (1.25, 1.25, 12.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          15.01877466]\n",
      "Spacing: (1.25, 1.25, 12.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          15.01877466]\n",
      "Spacing: (1.25, 1.25, 12.000000953674316) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          15.01877466]\n",
      "./cmr_MR_normalized/image_32.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 9.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         11.2640801]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 9.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         11.2640801]\n",
      "Spacing: (1.25, 1.25, 9.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         11.2640801]\n",
      "Spacing: (1.25, 1.25, 9.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         11.2640801]\n",
      "Spacing: (1.25, 1.25, 9.0) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.        256.         11.2640801]\n",
      "./cmr_MR_normalized/image_33.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 10.99999713897705) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720543]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 10.99999713897705) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720543]\n",
      "Spacing: (1.25, 1.25, 10.99999713897705) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720543]\n",
      "Spacing: (1.25, 1.25, 10.99999713897705) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720543]\n",
      "Spacing: (1.25, 1.25, 10.99999713897705) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 10) -> [256.         256.          13.76720543]\n",
      "./cmr_MR_normalized/image_34.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "./cmr_MR_normalized/image_35.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "Spacing: (1.1875, 1.1875, 7.999999523162842) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 10) -> [304.         304.          10.01251505]\n",
      "./cmr_MR_normalized/image_4.nii.gz has been saved\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 8.000004768371582) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01502593]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 8.000004768371582) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01502593]\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 8.000004768371582) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01502593]\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 8.000004768371582) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01502593]\n",
      "Spacing: (1.2152777910232544, 1.2152777910232544, 8.000004768371582) -> (1.25, 1.25, 7.99)\n",
      "Size (288, 288, 12) -> [280.00000305 280.00000305  12.01502593]\n",
      "./cmr_MR_normalized/image_5.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 12.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.         256.          12.01502164]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 12.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.         256.          12.01502164]\n",
      "Spacing: (1.25, 1.25, 12.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.         256.          12.01502164]\n",
      "Spacing: (1.25, 1.25, 12.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.         256.          12.01502164]\n",
      "Spacing: (1.25, 1.25, 12.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 8) -> [256.         256.          12.01502164]\n",
      "./cmr_MR_normalized/image_6.nii.gz has been saved\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "Spacing: (1.1875, 1.1875, 8.000001907348633) -> (1.25, 1.25, 7.99)\n",
      "Size (320, 320, 12) -> [304.         304.          12.01502164]\n",
      "./cmr_MR_normalized/image_7.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502307]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502307]\n",
      "Spacing: (1.25, 1.25, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502307]\n",
      "Spacing: (1.25, 1.25, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502307]\n",
      "Spacing: (1.25, 1.25, 8.00000286102295) -> (1.25, 1.25, 7.99)\n",
      "Size (256, 256, 12) -> [256.         256.          12.01502307]\n",
      "./cmr_MR_normalized/image_8.nii.gz has been saved\n",
      "Spacing: (1.25, 1.25, 7.999997615814209) -> (1.25, 1.25, 7.99)\n",
      "Size (240, 240, 12) -> [240.         240.          12.01501519]\n",
      "Label values: [  0 200 500 600]\n",
      "Spacing: (1.25, 1.25, 7.999997615814209) -> (1.25, 1.25, 7.99)\n",
      "Size (240, 240, 12) -> [240.         240.          12.01501519]\n",
      "Spacing: (1.25, 1.25, 7.999997615814209) -> (1.25, 1.25, 7.99)\n",
      "Size (240, 240, 12) -> [240.         240.          12.01501519]\n",
      "Spacing: (1.25, 1.25, 7.999997615814209) -> (1.25, 1.25, 7.99)\n",
      "Size (240, 240, 12) -> [240.         240.          12.01501519]\n",
      "Spacing: (1.25, 1.25, 7.999997615814209) -> (1.25, 1.25, 7.99)\n",
      "Size (240, 240, 12) -> [240.         240.          12.01501519]\n",
      "./cmr_MR_normalized/image_9.nii.gz has been saved\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "try:\n",
    "    os.mkdir(OUT_FOLDER)\n",
    "except:\n",
    "    print(\"Failed to create the output folder.\")\n",
    "    \n",
    "HIST_CUT_TOP = 0.5 # cut top 0.5% of intensity historgam to alleviate off-resonance effect\n",
    "\n",
    "NEW_SPA = (1.25, 1.25, 7.99) # unified voxel spacing\n",
    "\n",
    "for img_fid, seg_fid, pid in zip(imgs, segs, pids):\n",
    "\n",
    "    lb_n = nio.read_nii_bysitk(seg_fid)\n",
    "    resample_flg = True\n",
    "\n",
    "    img_obj = sitk.ReadImage( img_fid )\n",
    "    seg_obj = sitk.ReadImage( seg_fid )\n",
    "\n",
    "    array = sitk.GetArrayFromImage(img_obj)\n",
    "\n",
    "    # cut histogram\n",
    "    hir = float(np.percentile(array, 100.0 - HIST_CUT_TOP))\n",
    "    array[array > hir] = hir\n",
    "\n",
    "    his_img_o = sitk.GetImageFromArray(array)\n",
    "    his_img_o = copy_spacing_ori(img_obj, his_img_o)\n",
    "\n",
    "    # resampling\n",
    "    img_spa_ori = img_obj.GetSpacing()\n",
    "    res_img_o = resample_by_res(his_img_o, NEW_SPA, interpolator = sitk.sitkLinear, logging = True)\n",
    "\n",
    "\n",
    "\n",
    "    ## label\n",
    "    lb_arr = sitk.GetArrayFromImage(seg_obj)\n",
    "\n",
    "    # resampling\n",
    "    res_lb_o = resample_lb_by_res(seg_obj,  NEW_SPA, interpolator = sitk.sitkLinear,\n",
    "                                  ref_img = None, logging = True)\n",
    "\n",
    "\n",
    "    # crop out rois\n",
    "    res_img_a = s2n(res_img_o)\n",
    "\n",
    "    crop_img_a = image_crop(res_img_a.transpose(1,2,0), [256, 256],\n",
    "                            referece_ctr_idx = [res_img_a.shape[1] // 2, res_img_a.shape[2] //2],\n",
    "                            padval = res_img_a.min(), only_2d = True).transpose(2,0,1)\n",
    "\n",
    "    out_img_obj = copy_spacing_ori(res_img_o, sitk.GetImageFromArray(crop_img_a))\n",
    "\n",
    "    res_lb_a = s2n(res_lb_o)\n",
    "\n",
    "    crop_lb_a = image_crop(res_lb_a.transpose(1,2,0), [256, 256],\n",
    "                            referece_ctr_idx = [res_lb_a.shape[1] // 2, res_lb_a.shape[2] //2],\n",
    "                            padval = 0, only_2d = True).transpose(2,0,1)\n",
    "\n",
    "    out_lb_obj = copy_spacing_ori(res_img_o, sitk.GetImageFromArray(crop_lb_a))\n",
    "\n",
    "\n",
    "    out_img_fid = os.path.join( OUT_FOLDER, f'image_{pid}.nii.gz' )\n",
    "    out_lb_fid  = os.path.join( OUT_FOLDER, f'label_{pid}.nii.gz' ) \n",
    "\n",
    "    # then save pre-processed images\n",
    "    sitk.WriteImage(out_img_obj, out_img_fid, True) \n",
    "    sitk.WriteImage(out_lb_obj, out_lb_fid, True) \n",
    "    print(\"{} has been saved\".format(out_img_fid))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
